package com.autumn.util;

import java.io.File;
import java.io.IOException;

import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

/**
 * 资源帮助
 * 
 * @author 老码农 2019-04-21 17:45:22
 */
public class ResourceUtils {

	/**
	 * 获取资源的根路径
	 * 
	 * @return
	 */
	public static String getResourceRootPath() {
		try {
			Resource[] resources = new PathMatchingResourcePatternResolver()
					.getResources(ResourceLoader.CLASSPATH_URL_PREFIX);
			for (Resource resource : resources) {
				if (resource.exists()) {
					String rootPath = resource.getFile().getAbsolutePath().replace("\\", "/");
					File dir = new File(rootPath);
					if (!dir.exists() || !dir.isDirectory()) {
						dir.mkdirs();
					}
					return rootPath;
				}
			}
			return "";
		} catch (IOException e) {
			return "";
		}
	}
}
